#!/usr/bin/env gawk -f
# Author: Ton Rullmann

# jfd@Fri Jan 14 14:43:02 CST 2000
# Moved the file name generation outside the BEGIN statement
# maybe we have an awk version not supporting it.

BEGIN {
    hdrfile = "split.tmp" PROCINFO["pid"]
    #print "DEBUG: using hdrfile: " hdrfile
    # JFD@Mon Mar 13 14:43:03 CST 2000 adds touch to prevent an error message 
    # from file not found when hdrfile is not in the same directory
    system( "touch " hdrfile )    

    if ( ARGC <= 1 ) {
	print "usage: splitpdb [modelnum=?] file"
	print ""
	print "Split a multi-model PDB file into separate PDB files, named xxx_001.yyy etc."
	print "where xxx is the name of the input file (minus directory path)"
	print "and yyy its extension (if present)."
	print ""
	print "If modelnum is given, only the model with the corresponding model number"
	print "is extracted."
	print ""
	print "Everything before the first MODEL (except JRNL and REMARK records)"
	print "is copied to every file."
	print "Everything between MODEL and ENDMDL is copied."
	print "The MODEL records are replaced by a REMARK."
	print "The ENDMDL records are replaced by END."
	print "Everything after the last ENDMDL (including CONECT's) is discarded."
	print "A MASTER record is not added."
	print ""
	print "If no MODEL records are present no new PDB files are produced"
	print "and the script exits with status 1"
	print "A MODEL 0 record (as in entry 1CRQ) is ignored by the script."
	exit( 1 )
    }
# split at directory separators
    n = split( FILENAME, tmp, "/" )
# split at the extension
    #nInpPart = split( tmp[n], inp, "\." ) # the new gawk on tang.bmrb.wisc.edu had a problem with it.
    nInpPart = split( tmp[n], inp, "." )
    model = ""
}

function fileName( nModel ) {

  if ( nModel == 1 ) then
# split at directory separators
      n = split( FILENAME, tmp, "/" )
# split at the extension
      #nInpPart = split( tmp[n], inp, "\." )
      nInpPart = split( tmp[n], inp, "." )
  endif

# form the file name, leaving out the directory path,
# but adding the extension (if present)
    if ( nInpPart > 1 )
	file = inp[1] "_" nModel "." inp[2]
    else
	file = inp[1] "_" nModel 
}

/^JRNL/ { next }

/^REMARK/ { next }

/^MODEL/ {
    modelx = $2

    ## JFD adds: Special case for entry 1crq with MODEL 0 record
    ## and       Special case for entry 1crr with MODEL 21 record
    ## Skip the whole model, treat it as if it is not an ensemble.
    ## if the first model listed is not number 1
    if ( model == "" ) 
    {
        if ( modelx != 1 ) next
    }
    
    model = sprintf( "%03i", $2 )
    if ( modelnum ) {
	if ( modelx > modelnum ) exit
	if ( modelx < modelnum ) next
    }
    fileName( model )
    print "copying model",$2,"to",file
    system( "/bin/cat -s " hdrfile ">" file )
    print "REMARK model", $2 >> file
    next
}

/^ENDMDL/ { 
    if ( modelnum ) {
	if ( modelx != modelnum ) next
    }
    if ( file != "" ) print "END" >> file
    next
}

/^CONECT/ { exit }

/^MASTER/ { exit }

{
    if ( model == "" )
	print $0 > hdrfile
    else {
	if ( modelnum ) {
	    if ( modelx != modelnum ) next
	}
	print $0 >> file
    }
}

END {
    system( "/bin/rm -f " hdrfile )
    if ( model == "" ) exit( 1 )
}
